package org.linlinjava.litemall.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import net.sf.json.JSONObject;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.dto.MemberNotifyDto;
import org.linlinjava.litemall.core.system.SystemConfig;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.beans.Constants;
import org.linlinjava.litemall.db.dto.LitemallSystemUpdateFormDto;
import org.linlinjava.litemall.db.service.LitemallSystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 管理配置控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/config")
@Validated
public class AdminConfigController {

    /**
     * 系统配置服务
     */
    @Autowired
    private LitemallSystemConfigService systemConfigService;

    /**
     * 购物中心列表
     *
     * @return {@link Object}
     */
    @GetMapping("/mall")
    @LogAnno
    public Object listMall() {
        Map<String, String> data = systemConfigService.listMail();
        return ResponseUtil.ok(data);
    }

    /**
     * 更新商场
     *
     * @param body 身体
     * @return {@link Object}
     */
    @PostMapping("/mall")
    @LogAnno
    public Object updateMall(@RequestBody String body) {
        Map<String, String> data = JacksonUtil.toMap(body);
        systemConfigService.updateConfig(data);
        SystemConfig.updateConfigs(data);
        return ResponseUtil.ok();
    }

    /**
     * 列表表达
     *
     * @return {@link Object}
     */
    @GetMapping("/express")
    @LogAnno
    public Object listExpress() {
        Map<String, String> data = systemConfigService.listExpress();
        return ResponseUtil.ok(data);
    }

    /**
     * 更新表达
     *
     * @param body 身体
     * @return {@link Object}
     */
    @PostMapping("/express")
    @LogAnno
    public Object updateExpress(@RequestBody String body) {
        Map<String, String> data = JacksonUtil.toMap(body);
        systemConfigService.updateConfig(data);
        SystemConfig.updateConfigs(data);
        return ResponseUtil.ok();
    }

    /**
     * lis秩序
     *
     * @return {@link Object}
     */
    @GetMapping("/order")
    @LogAnno
    public Object lisOrder() {
        Map<String, String> data = systemConfigService.listOrder();
        return ResponseUtil.ok(data);
    }

    /**
     * 更新订单
     *
     * @param body 身体
     * @return {@link Object}
     */
    @PostMapping("/order")
    @LogAnno
    public Object updateOrder(@RequestBody String body) {
        Map<String, String> data = JacksonUtil.toMap(body);
        systemConfigService.updateConfig(data);
        return ResponseUtil.ok();
    }

    /**
     * lis数量
     *
     * @return {@link Object}
     */
    @GetMapping("/amount")
    @LogAnno
    public Object lisAmount() {
        Map<String, String> data = systemConfigService.listAmount();
        return ResponseUtil.ok(data);
    }

    /**
     * 更新数量
     *
     * @param body 身体
     * @return {@link Object}
     */
    @PostMapping("/amount")
    @LogAnno
    public Object updateAmount(@RequestBody String body) {
        Map<String, String> data = JacksonUtil.toMap(body);
        systemConfigService.updateConfig(data);
        SystemConfig.updateConfigs(data);
        return ResponseUtil.ok();
    }

    /**
     * 结算清单
     *
     * @return {@link Object}
     */
    @GetMapping("/settlement")
    @LogAnno
    public Object listSettlement() {
        return ResponseUtil.ok(systemConfigService.listSettlement());
    }

    /**
     * 更新解决
     *
     * @param body 身体
     * @return {@link Object}
     */
    @PostMapping("/settlement")
    @LogAnno
    public Object updateSettlement(@RequestBody String body) {
        Map<String, String> data = JacksonUtil.toMap(body);
        systemConfigService.updateConfig(data);
        SystemConfig.updateConfigs(data);
        return ResponseUtil.ok();
    }

    /**
     * 系统
     *
     * @param keyName 键名
     * @return {@link Object}
     */
    @GetMapping("/system")
    @LogAnno
    public Object system(String keyName) {
        return ResponseUtil.ok(systemConfigService.queryByKeyName(keyName));
    }

    /**
     * 更新积分
     *
     * @param forms 形式
     * @return {@link Object}
     */
    @PostMapping("/system")
    @LogAnno
    public Object updateIntegral(@RequestBody List<LitemallSystemUpdateFormDto> forms) {
		forms.forEach(e -> {
			Map<String, String> map = new HashMap<>();
			map.put(e.getKeyName(), e.getKeyValue());
			SystemConfig.updateConfigs(map);
		});
		systemConfigService.updateMultiple(forms);
        return ResponseUtil.ok();
    }

    /**
     * 支付通知成员
     *
     * @param dto dto
     * @return {@link Object}
     */
    @PutMapping("/payMemberNotify")
    @LogAnno
    public Object payMemberNotify(@RequestBody MemberNotifyDto dto){
        String systemValue =  systemConfigService.queryByKeyName(Constants.SYSTEM_PAY_MEMBER);
        String dtoString = JSON.toJSONString(dto);
        if(systemValue == null){
            systemConfigService.addConfig(Constants.SYSTEM_PAY_MEMBER,dtoString);
        }else{
            Map<String,String> map = new HashMap<>();
            map.put(Constants.SYSTEM_PAY_MEMBER,dtoString);
            systemConfigService.updateConfig(map);
        }
        return ResponseUtil.ok("修改付费会员到期提醒设置成功");
    }


    /**
     * 级成员通知
     *
     * @param dto dto
     * @return {@link Object}
     */
    @PutMapping("/levelMemberNotify")
    @LogAnno
    public Object levelMemberNotify(@RequestBody MemberNotifyDto dto){
       String systemValue =  systemConfigService.queryByKeyName(Constants.SYSTEM_LEVEL_MEMBER);
        String dtoString = JSON.toJSONString(dto);
        if(systemValue == null){
           systemConfigService.addConfig(Constants.SYSTEM_LEVEL_MEMBER,dtoString);
        }else{
            Map<String,String> map = new HashMap<>();
            map.put(Constants.SYSTEM_LEVEL_MEMBER,dtoString);
            systemConfigService.updateConfig(map);
        }
        return ResponseUtil.ok("修改等级会员到期提醒设置成功");
    }

    /**
     * 得到通知支付元素
     *
     * @return {@link Object}
     */
    @GetMapping("/getPayMemberNotify")
    @LogAnno
    public Object getPayMemberNotify(){
        String systemValue =  systemConfigService.queryByKeyName(Constants.SYSTEM_PAY_MEMBER);
        JSONObject jsonObject1 = JSONObject.fromObject(systemValue);
        MemberNotifyDto payMemberNotify = (MemberNotifyDto) JSONObject.toBean(jsonObject1,MemberNotifyDto.class);
        return ResponseUtil.ok(payMemberNotify);
    }

    /**
     * 得到通知水平元素
     *
     * @return {@link Object}
     */
    @GetMapping("/getLevelMemberNotify")
    @LogAnno
    public Object getLevelMemberNotify(){
        String systemValue =  systemConfigService.queryByKeyName(Constants.SYSTEM_LEVEL_MEMBER);
        JSONObject jsonObject1 = JSONObject.fromObject(systemValue);
        MemberNotifyDto levelMemberNotify = (MemberNotifyDto) JSONObject.toBean(jsonObject1,MemberNotifyDto.class);
        return ResponseUtil.ok(levelMemberNotify);
    }

    /**
     * 支付类型列表
     *
     * @return {@link Object}
     */
    @GetMapping("/payType")
    @LogAnno
    public Object listPayType() {
        Map<String, String> data = systemConfigService.listPayType();
        return ResponseUtil.ok(data);
    }

    /**
     * 更新支付类型
     *
     * @param body 身体
     * @return {@link Object}
     */
    @PostMapping("/payType")
    @LogAnno
    public Object updatePayType(@RequestBody String body) {
        Map<String, String> data = JacksonUtil.toMap(body);
        systemConfigService.updateConfigPayType(data);
        SystemConfig.updateConfigs(data);
        return ResponseUtil.ok();
    }
}
